//index.js
const Controllers = require('../../controllers/Controller.js').controller;
const app = getApp();
Page({
  data: { 
    agentType: 0,
    tabs: [
      {
        text: '全部',
        status: 0
      },
      {
        text: '待审核',
        status: 2
      },
      {
        text: '审核失败',
        status: 3
      }
    ],
    active: 0
  },
  onLoad(options) {
    console.log('options==', options);
    var that = this;
    that.setData({
      active: parseInt(options.active) || 0
    })
  },
  onShow() {
    var that = this;

    app.judgeAgent(type => {
      if (type === 1) {
       that.myShop();
     } else if (type === 0) {
       that.staffMyShop();
     } else {
       wx.showToast({
         title: '代理商员工待接入',
         icon: 'none'
       })
     }
     that.setData({
       agentType: type
     })
   })
  },
  // 代理商
  myShop() {
    var that = this;
    wx.showLoading();
    var active = that.data.active;
    var tabs = that.data.tabs;
    var state = tabs[active].status;
    var params = {};
    if (state) {
      params.state = state;
    }
    Controllers.myShop(params).then(res => {
      console.log('myShop--res==', res);
      const { code, data, desc } = res;
      that.setData({
        list: data
      });
      wx.hideLoading()
    })
  },
  // 代理商员工；
  staffMyShop() {
    var that = this;
    wx.showLoading();
    var active = that.data.active;
    var tabs = that.data.tabs;
    var state = tabs[active].status;
    var params = {};
    if (state) {
      params.state = state;
    }
    Controllers.staffMyShop(params).then(res => {
      console.log('staffMyShop--res==', res);
      const { code, data, desc } = res;
      that.setData({
        list: data
      });
      wx.hideLoading()
    })
  },

  changeTab(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    if (index !== that.data.active) {
      that.setData({
        active: index
      })
      if (that.data.agentType === 1) {
        that.myShop();
      } else if (that.data.agentType === 0) {
        that.staffMyShop();
      }
    }

  },
  openPage(e) {
    var url = e.currentTarget.dataset.url;
    if (url) {
      app.openPage({ url });
    } else {
      wx.showToast({
        title: '未获取页面路径',
        icon: 'none'
      });
    }
  }
})